home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 1999 August / SGI Freeware 1999 August.iso / dist / fw_gnome-python.idb / usr / freeware / lib / python1.5 / site-packages / pyglade / build.py.z / build.py
Encoding:
Python Source  |  1999-07-16  |  24.5 KB  |  728 lines

  1. """This module is responsible for creating widgets from a
  2. pyglade.xmhtml.TagTree.  As opposed to the C code generation approach,
  3. this will build the widget tree at runtime.  There is nothing wrong with
  4. the code generation approach, but this one is what I have decided to do.
  5. I may write a code generator at some later point (the tag tree and style
  6. generators are sufficiently general).
  7. """
  8.  
  9. import sys
  10. import string
  11. from gtk import *
  12. # the style sheet generator
  13. import style
  14.  
  15. error = "pyglade.build.error"
  16.  
  17. # This is a dictionary of widget creation routines.
  18. # It should have string keys representing widget classes, and a tuple as
  19. # the value.  The first function is for creation of this widget type.
  20. # It should take one argument (the TagTree node), and returns a widget
  21. # with those attributes.  Note that it doesn't have to set the widget
  22. # names or connect signal handlers (in fact it shouldn't do this).
  23. #
  24. # The second function is for adding a child to this container widget.  If
  25. # this widget class is not a container, the second item in the tuple should
  26. # be None.  If it is a container, the function should take as arguments the
  27. # container, the child, and finally the 'child' TagTree node corresponding to
  28. # the child widget (or None if there is no child TagTree node).
  29.  
  30. _widgets = {}
  31.  
  32. class WidgetTree:
  33.     def __init__(self, tree):
  34.         if tree.has_key('gtk-interface'): tree = tree['gtk-interface']
  35.         if tree.tag != 'gtk-interface':
  36.             raise error, "first argument not the base of tag tree"
  37.         self.__signals = {}  # signals
  38.         self.__widgets = {}  # widgets by name
  39.         self.__paths = {}    # widgets by path
  40.         self.tooltips = None
  41.  
  42.         # parse rc strings
  43.         if tree.has_key('style'):
  44.             rc_parse_string(style.as_string(tree))
  45.  
  46.         # create widgets
  47.         if tree.has_key('widget'):
  48.             children = tree.widget
  49.             if type(children) != type(()): children = (children,)
  50.             for child in children:
  51.                 self.__new_widget(child)
  52.         if self.tooltips:
  53.             self.tooltips.enable();
  54.  
  55.     def get_widget(self, name):
  56.         if string.find(name, '.') == -1:  # no dot
  57.             return self.__widgets[name]
  58.         else:
  59.             return self.__paths[name]
  60.  
  61.     def connect(self, signame, handler, *data):
  62.         self.__signals[signame] = (self.__signals[signame][0], data,
  63.                        handler)
  64.     def disconnect(self, signame):
  65.         self.__signals[signame] = (self.__signals[signame][0], (),None)
  66.  
  67.     def __signal_handler(self, wid, *args):
  68.         signame = args[-1]
  69.         args = args[:-1]
  70.         objname, data, handler = self.__signals[signame]
  71.         if not handler: return
  72.         obj = self.__widgets[objname]
  73.         return apply(handler, (obj,) + args + data)
  74.         
  75.     def __new_widget(self, node, parent=''):
  76.         path = parent + '.' + node.name
  77.         wclass = node['class']
  78.         if wclass == 'Placeholder':
  79.             sys.stderr.write(
  80.                 "warning: placeholders still in description\n")
  81.             return
  82.         create, add = _widgets[wclass]
  83.         widget = create(node)
  84.         if node.has_key('name'): widget.set_name(node.name)
  85.         self.__widgets[node.name] = widget
  86.         self.__paths[path[:-1]] = widget
  87.         self.__set_common_params(widget, node)
  88.  
  89.         if node.has_key('signal'):
  90.             self.__setup_sighandlers(widget, node)
  91.         if node.has_key('widget'):
  92.             if not add:
  93.                 raise error, "don't know how to add " + \
  94.                       "widgets to this container"
  95.             children = node.widget
  96.             if type(children) != type(()): children = (children,)
  97.             for child in children:
  98.                 self.__new_widget(child, parent=path)
  99.                 cwidget = self.get_widget(child.name)
  100.                 add(widget, cwidget, child)
  101.  
  102.                 # these can only be set after packing
  103.                 has_default = child.get_bool('has_default',
  104.                                  FALSE)
  105.                 if has_default: cwidget.grab_default()
  106.                 has_focus = child.get_bool('has_focus', FALSE)
  107.                 if has_focus: cwidget.grab_focus()
  108.  
  109.         if node.get_bool('visible', TRUE):
  110.             widget.show()
  111.  
  112.     def __set_common_params(self, widget, node):
  113.         width = node.get_int('width', -1)
  114.         height = node.get_int('height', -1)
  115.         if width != -1 or height != -1:
  116.             widget.set_usize(width, height)
  117.         sensitive = node.get_bool('sensitive', TRUE)
  118.         if not sensitive:
  119.             widget.set_sensitive(FALSE)
  120.         tooltip = node.get('tooltip', None)
  121.         if tooltip:
  122.             if not self.tooltips: self.tooltips = GtkTooltips()
  123.             self.tooltips.set_tip(widget, tooltip, '')
  124.         can_default = node.get_bool('can_default', FALSE)
  125.         if can_default: widget.set_flags(CAN_DEFAULT)
  126.         can_focus = node.get_bool('can_focus', FALSE)
  127.         if can_focus: widget.set_flags(CAN_FOCUS)
  128.         events = node.get_int('events', 0)
  129.         if events: widget.set_events(events)
  130.         extension = node.get('extension_events', None)
  131.         if extension: widget.set_extension_events(extension)
  132.         border_width = node.get_int('border_width', 0)
  133.         if border_width: widget.set_border_width(border_width)
  134.  
  135.     def __setup_sighandlers(self, widget, node):
  136.         signals = node.signal
  137.         if type(signals) != type(()): signals = (signals,)
  138.         for sig in signals:
  139.             signame = sig.name
  140.             self.__signals[sig.handler] = (
  141.                     sig.get('object', node.name), (), None)
  142.             if sig.get_bool('after', FALSE):
  143.                 widget.connect_after(signame,
  144.                              self.__signal_handler,
  145.                              sig.handler)
  146.             else:
  147.                 widget.connect(signame, self.__signal_handler,
  148.                            sig.handler)
  149.  
  150.  
  151. # These functions handle adding elements to the various container types
  152. def container_add(cont, child, info):
  153.     cont.add(child)
  154. def box_add(box, child, info):
  155.     info = info.child
  156.     expand = info.get_bool('expand', TRUE)
  157.     fill = info.get_bool('fill', TRUE)
  158.     padding = info.get_int('padding', 0)
  159.     if info.get('pack', 'GTK_PACK_START') == 'GTK_PACK_START':
  160.         box.pack_start(child, expand=expand, fill=fill,padding=padding)
  161.     else:
  162.         box.pack_end(child, expand=expand, fill=fill,padding=padding)
  163. def table_add(table, child, info):
  164.     info = info.child
  165.     la = info.get_int('left_attach', 0)
  166.     ra = info.get_int('right_attach', 1)
  167.     ta = info.get_int('top_attach', 0)
  168.     ba = info.get_int('bottom_attach', 1)
  169.     xpad = info.get_int('xpad', 0)
  170.     ypad = info.get_int('ypad', 0)
  171.     xoptions=0
  172.     yoptions=0
  173.     if info.get_bool('xexpand', TRUE):  xoptions = xoptions | EXPAND
  174.     if info.get_bool('xshrink', FALSE): xoptions = xoptions | SHRINK
  175.     if info.get_bool('xfill', TRUE):    xoptions = xoptions | FILL
  176.     if info.get_bool('yexpand', TRUE):  yoptions = yoptions | EXPAND
  177.     if info.get_bool('yshrink', FALSE): yoptions = yoptions | SHRINK
  178.     if info.get_bool('yfill', TRUE):    yoptions = yoptions | FILL
  179.     table.attach(child, la,ra, ta,ba, xoptions=xoptions, yoptions=yoptions,
  180.              xpadding=xpad, ypadding=ypad)
  181. def fixed_add(fix, child, info):
  182.     x = info.get_int('x', 0)
  183.     y = info.get_int('y', 0)
  184.     fix.put(child, x, y)
  185. def clist_add(clist, child, info):
  186.     col = info.parent.get('col_no', 0)
  187.     info.parent['col_no'] = col + 1
  188.     clist.set_column_widget(col, child)
  189. def paned_add(paned, child, info):
  190.     pane = info.parent.get('pane2', FALSE)
  191.     info.parent['pane2'] = TRUE
  192.     if not pane:
  193.         paned.add1(child)
  194.     else:
  195.         paned.add2(child)
  196. def notebook_add(book, child, info):
  197.     pages = info.parent.pages
  198.     if not info.has_key('child_name') or info.child_name != 'Notebook:tab':
  199.         pages.append(child)
  200.     else:
  201.         # child is a label
  202.         book.append_page(pages[0], child)
  203.         del pages[0]
  204. def dialog_add(dlg, child, info):
  205.     # the widgets are already added
  206.     pass
  207.  
  208. def menuitem_add(mi, menu, info):
  209.     mi.set_submenu(menu)
  210. def menushell_add(menu, mi, info):
  211.     menu.append(mi)
  212.  
  213. def misc_set(misc, info):
  214.     xalign = info.get_float('xalign', 0.5)
  215.     yalign = info.get_float('yalign', 0.5)
  216.     misc.set_alignment(xalign, yalign)
  217.     xpad = info.get_int('xpad', 0)
  218.     ypad = info.get_int('ypad', 0)
  219.     misc.set_padding(xpad, ypad)
  220.  
  221.  
  222. def label_new(node):
  223.     str = node.get('label', '')
  224.     label = GtkLabel(str)
  225.     misc_set(label, node)
  226.     just = node.get('justify', JUSTIFY_CENTER)
  227.     label.set_justify(just)
  228.     return label
  229. def entry_new(node):
  230.     ent = GtkEntry(maxlen=node.get_int('text_max_length', -1))
  231.     if not node.get_bool('editable', TRUE):
  232.         ent.set_editable(FALSE)
  233.     if not node.get_bool('text_visible', TRUE):
  234.         ent.set_visibility(FALSE)
  235.     text = node.get('text', '')
  236.     if text: ent.set_text(text)
  237.     return ent
  238. def text_new(node):
  239.     text = GtkText()
  240.     if not node.get_bool('editable', TRUE):
  241.         text.get_editable(FALSE)
  242.     t = node.get('text', '')
  243.     if t:
  244.         text.insert_text(t)
  245.     return text
  246. def button_new(node):
  247.     label = node.get('label', '')
  248.     return GtkButton(label)
  249. def togglebutton_new(node):
  250.     label = node.get('label', '')
  251.     tog = GtkToggleButton(label)
  252.     if node.get_bool('active', FALSE):
  253.         tog.set_state(TRUE)
  254.     return tog
  255. def checkbutton_new(node):
  256.     label = node.get('label', '')
  257.     cb = GtkCheckButton(label)
  258.     if node.get_bool('active', FALSE):
  259.         cb.set_state(TRUE)
  260.     if not node.get_bool('draw_indicator', TRUE):
  261.         cb.set_mode(FALSE)
  262.     return cb
  263. def radiobutton_new(node):
  264.     label = node.get('label', '')
  265.     # do something about radio button groups ...
  266.     rb = GtkRadioButton(label)
  267.     if node.get_bool('active', FALSE):
  268.         rb.set_state(TRUE)
  269.     if not node.get_bool('draw_indicator', TRUE):
  270.         rb.set_mode(FALSE)
  271.     return rb
  272. def optionmenu_new(node):
  273.     omenu = GtkOptionMenu()
  274.     menu = GtkMenu()
  275.     for item in string.split(node.get_string('items', ''), '\n'):
  276.         mi = GtkMenuItem(item)
  277.         menu.append(mi)
  278.         mi.show()
  279.     omenu.set_menu(menu)
  280.     return omenu
  281. def combo_new(node):
  282.     combo = GtkCombo()
  283.     if node.get_bool('case_sensitive', FALSE):
  284.         combo.set_case_sensitive(TRUE)
  285.     if not node.get_bool('use_arrows', TRUE):
  286.         combo.set_use_arrows(FALSE)
  287.     if node.get_bool('use_arrows_always', FALSE):
  288.         combo.set_use_arrows_always(TRUE)
  289.     return combo
  290. def list_new(node):
  291.     list = GtkList()
  292.     mode = node.get('selection_mode', SELECTION_SINGLE)
  293.     list.set_selection_mode(mode)
  294.     return list
  295. def clist_new(node):
  296.     numcols = node.get_int('columns', 1)
  297.     clist = GtkCList(numcols)
  298.     widths = node.get('column_widths', None)
  299.     if widths:
  300.         widths = map(string.atoi, string.split(widths, ','))
  301.         for i in range(numcols):
  302.             clist.set_column_width(i, widths[i])
  303.     if node.get_bool('show_titles', TRUE):
  304.         clist.column_titles_show()
  305.     mode = node.get('selection_mode', SELECTION_SINGLE)
  306.     clist.set_selection_mode(mode)
  307.     #shadow = node.get('shadow_type', SHADOW_IN)
  308.     #clist.set_border(shadow)
  309.     #hpol = node.get('hscrollbar_policy', POLICY_ALWAYS)
  310.     #vpol = node.get('vscrollbar_policy', POLICY_ALWAYS)
  311.     #clist.set_policy(hpol, vpol)
  312.     return clist
  313. def tree_new(node):
  314.     tree = GtkTree()
  315.     mode = node.get('selection_mode', SELECTION_SINGLE)
  316.     tree.set_selection_mode(mode)
  317.     mode = node.get('view_mode', TREE_VIEW_LINE)
  318.     tree.set_view_mode(mode)
  319.     if not node.get_bool('view_line', TRUE):
  320.         tree.set_view_lines(FALSE)
  321.     return tree
  322. def spinbutton_new(node):
  323.     climb_rate = node.get_int('climb_rate', 1)
  324.     digits = node.get_int('digits', 0)
  325.     hvalue = node.get_float('hvalue', 1)
  326.     hlower = node.get_float('hlower', 0)
  327.     hupper = node.get_float('hupper', 100)
  328.     hstep  = node.get_float('hstep', 1)
  329.     hpage  = node.get_float('hpage', 10)
  330.     hpage_size = node.get_float('hpage_size', 10)
  331.     adj = GtkAdjustment(hvalue, hlower, hupper, hstep, hpage, hpage_size)
  332.     spin = GtkSpinButton(adj=adj, climb_rate=climb_rate, digits = digits)
  333.     spin.set_numeric(node.get_bool('numeric', FALSE))
  334.     pol = node.get('update_policy', UPDATE_IF_VALID)
  335.     spin.set_update_policy(pol)
  336.     spin.set_snap_to_ticks(node.get_bool('snap', FALSE))
  337.     spin.set_wrap(node.get_bool('wrap', FALSE))
  338.     return spin
  339. def hscale_new(node):
  340.     hvalue = node.get_float('hvalue', 1)
  341.     hlower = node.get_float('hlower', 0)
  342.     hupper = node.get_float('hupper', 100)
  343.     hstep  = node.get_float('hstep', 1)
  344.     hpage  = node.get_float('hpage', 10)
  345.     hpage_size = node.get_float('hpage_size', 10)
  346.     adj = GtkAdjustment(hvalue, hlower, hupper, hstep, hpage, hpage_size)
  347.     scale = GtkHScale(adj)
  348.     scale.set_draw_value(node.get_bool('draw_value', TRUE))
  349.     scale.set_value_pos(node.get('value_pos', POS_TOP))
  350.     scale.set_digits(node.get_int('digits', 1))
  351.     scale.set_update_policy(node.get('policy', UPDATE_CONTINUOUS))
  352.     return scale
  353. def vscale_new(node):
  354.     hvalue = node.get_float('hvalue', 1)
  355.     hlower = node.get_float('hlower', 0)
  356.     hupper = node.get_float('hupper', 100)
  357.     hstep  = node.get_float('hstep', 1)
  358.     hpage  = node.get_float('hpage', 10)
  359.     hpage_size = node.get_float('hpage_size', 10)
  360.     adj = GtkAdjustment(hvalue, hlower, hupper, hstep, hpage, hpage_size)
  361.     scale = GtkVScale(adj)
  362.     scale.set_draw_value(node.get_bool('draw_value', TRUE))
  363.     scale.set_value_pos(node.get('value_pos', POS_TOP))
  364.     scale.set_digits(node.get_int('digits', 1))
  365.     scale.set_update_policy(node.get('policy', UPDATE_CONTINUOUS))
  366.     return scale
  367. def hruler_new(node):
  368.     widget = GtkHRuler()
  369.     widget.set_metric(node.get('metric', PIXELS))
  370.     lower = node.get_int('lower', 0)
  371.     upper = node.get_int('upper', 10)
  372.     pos = node.get_int('position', 0)
  373.     max = node.get_int('max_size', 10)
  374.     widget.set_range(lower, upper, pos, max)
  375.     return widget
  376. def vruler_new(node):
  377.     widget = GtkVRuler()
  378.     widget.set_metric(node.get('metric', PIXELS))
  379.     lower = node.get_int('lower', 0)
  380.     upper = node.get_int('upper', 10)
  381.     pos = node.get_int('position', 0)
  382.     max = node.get_int('max_size', 10)
  383.     widget.set_range(lower, upper, pos, max)
  384.     return widget
  385. def hscrollbar_new(node):
  386.     hvalue = node.get_float('hvalue', 1)
  387.     hlower = node.get_float('hlower', 0)
  388.     hupper = node.get_float('hupper', 100)
  389.     hstep  = node.get_float('hstep', 1)
  390.     hpage  = node.get_float('hpage', 10)
  391.     hpage_size = node.get_float('hpage_size', 10)
  392.     adj = GtkAdjustment(hvalue, hlower, hupper, hstep, hpage, hpage_size)
  393.     scroll = GtkHScrollbar(adj)
  394.     scroll.set_update_policy(node.get('policy', UPDATE_CONTINUOUS))
  395.     return scroll
  396. def vscrollbar_new(node):
  397.     hvalue = node.get_float('hvalue', 1)
  398.     hlower = node.get_float('hlower', 0)
  399.     hupper = node.get_float('hupper', 100)
  400.     hstep  = node.get_float('hstep', 1)
  401.     hpage  = node.get_float('hpage', 10)
  402.     hpage_size = node.get_float('hpage_size', 10)
  403.     adj = GtkAdjustment(hvalue, hlower, hupper, hstep, hpage, hpage_size)
  404.     scroll = GtkVScrollbar(adj)
  405.     scroll.set_update_policy(node.get('policy', UPDATE_CONTINUOUS))
  406.     return scroll
  407. def statusbar_new(node):
  408.     return GtkStatusbar()
  409. def toolbar_new(node):
  410.     orient = node.get('orientation', ORIENTATION_HORIZONTAL)
  411.     style = node.get('type', TOOLBAR_ICONS)
  412.     tool = GtkToolbar(orient, style)
  413.     tool.set_space_size(node.get_int('space_size', 5))
  414.     tool.set_tooltips(node.get_bool('tooltips', TRUE))
  415.     return tool
  416. def progressbar_new(node):
  417.     return GtkProgressBar()
  418. def arrow_new(node):
  419.     dir = node.get('arrow_type', ARROW_RIGHT)
  420.     shadow = node.get('shadow_type', SHADOW_OUT)
  421.     arr = GtkArrow(dir, shadow)
  422.     misc_set(arr, node)
  423.     return arr
  424. # image not supported
  425. def pixmap_new(node):
  426.     # the first parameter needs to be the toplevel widget
  427.     pix,bit = create_pixmap_from_xpm(None, None, node.get('filename', ''))
  428.     pixmap = GtkPixmap(pix, bit)
  429.     misc_set(pixmap)
  430.     return pixmap
  431. def drawingarea_new(node):
  432.     return GtkDrawingArea()
  433. def hseparator_new(node):
  434.     return GtkHSeparator()
  435. def vseparator_new(node):
  436.     return GtkVSeparator()
  437.  
  438. def menubar_new(node):
  439.     return GtkMenuBar()
  440. def menu_new(node):
  441.     return GtkMenu()
  442. def menuitem_new(node):
  443.     if node.has_key('label'):
  444.         ret = GtkMenuItem(node.label)
  445.     else:
  446.         ret = GtkMenuItem()
  447.     if node.get_bool('right_justify', FALSE):
  448.         ret.right_justify()
  449.     return ret
  450. def checkmenuitem_new(node):
  451.     ret = GtkCheckMenuItem(node.label)
  452.     if node.get_bool('right_justify', FALSE):
  453.         ret.right_justify()
  454.     if node.get_bool('active', FALSE):
  455.         ret.set_state(TRUE)
  456.     if node.get_bool('always_show_toggle', FALSE):
  457.         ret.set_show_toggle(TRUE)
  458.     return ret
  459. def radiomenuitem_new(node):
  460.     ret = GtkRadioMenuItem(node.label)
  461.     if node.get_bool('right_justify', FALSE):
  462.         ret.right_justify()
  463.     if node.get_bool('active', FALSE):
  464.         ret.set_state(TRUE)
  465.     if node.get_bool('always_show_toggle', FALSE):
  466.         ret.set_show_toggle(TRUE)
  467.     return ret
  468.  
  469. def hbox_new(node):
  470.     if node.has_key('child_name') and node.child_name[:7] == 'Dialog:':
  471.         return node.__wid
  472.     homogeneous = node.get_bool('homogeneous', FALSE)
  473.     spacing = node.get_int('spacing', 0)
  474.     return GtkHBox(homogeneous=homogeneous, spacing=spacing)
  475. def vbox_new(node):
  476.     if node.has_key('child_name') and node.child_name[:7] == 'Dialog:':
  477.         return node.__wid
  478.     homogeneous = node.get_bool('homogeneous', FALSE)
  479.     spacing = node.get_int('spacing', 0)
  480.     return GtkVBox(homogeneous=homogeneous, spacing=spacing)
  481. def table_new(node):
  482.     rows = node.get_int('rows', 1)
  483.     cols = node.get_int('columns', 1)
  484.     homog = node.get_bool('homogeneous', FALSE)
  485.     table = GtkTable(rows, cols, homog)
  486.     table.set_row_spacings(node.get_int('row_spacing', 0))
  487.     table.set_col_spacings(node.get_int('col_spacing', 0))
  488.     return table
  489. def fixed_new(node):
  490.     return GtkFixed()
  491. def hbuttonbox_new(node):
  492.     bbox = GtkHButtonBox()
  493.     layout = node.get('layout_style', BUTTONBOX_DEFAULT_STYLE)
  494.     bbox.set_layout(layout)
  495.     spacing = node.get_int('child_min_width', None)
  496.     if spacing: bbox.set_spacing(spacing)
  497.     width, height = bbox.get_child_size_default()
  498.     width = node.get_int('child_min_width', width)
  499.     height = node.get_int('child_min_height', height)
  500.     bbox.set_child_size(width, height)
  501.     ipx, ipy = bbox.get_child_ipadding_default()
  502.     ipx = node.get_int('child_ipad_x', ipx)
  503.     ipy = node.get_int('child_ipad_y', ipy)
  504.     bbox.set_child_ipadding(ipx, ipy)
  505.     return bbox
  506. def vbuttonbox_new(node):
  507.     bbox = GtkVButtonBox()
  508.     layout = node.get('layout_style', BUTTONBOX_DEFAULT_STYLE)
  509.     bbox.set_layout(layout)
  510.     spacing = node.get_int('child_min_width', None)
  511.     if spacing: bbox.set_spacing(spacing)
  512.     width, height = bbox.get_child_size_default()
  513.     width = node.get_int('child_min_width', width)
  514.     height = node.get_int('child_min_height', height)
  515.     bbox.set_child_size(width, height)
  516.     ipx, ipy = bbox.get_child_ipadding_default()
  517.     ipx = node.get_int('child_ipad_x', ipx)
  518.     ipy = node.get_int('child_ipad_y', ipy)
  519.     bbox.set_child_ipadding(ipx, ipy)
  520.     return bbox
  521. def frame_new(node):
  522.     label = node.get('label', '')
  523.     frame = GtkFrame(label)
  524.     xalign = node.get_int('label_xalign', 0)
  525.     frame.set_label_align(xalign, 0.5)
  526.     shadow = node.get('shadow_type', SHADOW_ETCHED_IN)
  527.     frame.set_shadow_type(shadow)
  528.     return frame
  529. def aspectframe_new(node):
  530.     xalign = node.get_int('xalign', 0)
  531.     yalign = node.get_int('yalign', 0)
  532.     ratio = node.get_float('ratio', 1)
  533.     obey = node.get_bool('obey_child', TRUE)
  534.     frame = GtkAspectFrame(xalign, yalign, ratio, obey_child)
  535.     label = node.get('label', '')
  536.     if label: frame.set_label(label)
  537.     xalign = node.get_int('label_xalign', 0)
  538.     frame.set_label_align(xalign, 0.5)
  539.     shadow = node.get('shadow_type', SHADOW_ETCHED_IN)
  540.     frame.set_shadow_type(shadow)
  541.     return frame
  542. def hpaned_new(node):
  543.     paned = GtkHPaned()
  544.     handle_size = node.get_int('handle_size', 0)
  545.     if handle_size: paned.set_handle_size(handle_size)
  546.     gutter_size = node.get_int('gutter_size', 0)
  547.     if gutter_size: paned.set_gutter_size(gutter_size)
  548.     return paned
  549. def vpaned_new(node):
  550.     paned = GtkVPaned()
  551.     handle_size = node.get_int('handle_size', 0)
  552.     if handle_size: paned.set_handle_size(handle_size)
  553.     gutter_size = node.get_int('gutter_size', 0)
  554.     if gutter_size: paned.set_gutter_size(gutter_size)
  555.     return paned
  556. def handlebox_new(node):
  557.     return GtkHandleBox()
  558. def notebook_new(node):
  559.     book = GtkNotebook()
  560.     book.set_show_tabs(node.get_bool('show_tabs', TRUE))
  561.     book.set_show_border(node.get_bool('show_border', TRUE))
  562.     book.set_tab_pos(node.get('tab_pos', POS_TOP))
  563.     book.set_scrollable(node.get_bool('scrollable', FALSE))
  564.     book.set_tab_border(node.get_int('tab_border', 3))
  565.     book.popup_enable(node.get_bool('popup_enable', FALSE))
  566.     node['pages'] = []
  567.     return book
  568. def alignment_new(node):
  569.     xalign = node.get_float('xalign', 0.5)
  570.     yalign = node.get_float('yalign', 0.5)
  571.     xscale = node.get_float('xscale', 0)
  572.     yscale = node.get_float('yscale', 0)
  573.     return GtkAlignment(xalign, yalign, xscale, yscale)
  574. def eventbox_new(node):
  575.     return GtkEventBox()
  576. def scrolledwindow_new(node):
  577.     win = GtkScrolledWindow()
  578.     hpol = node.get('hscrollbar_policy', POLICY_ALWAYS)
  579.     vpol = node.get('vscrollbar_policy', POLICY_ALWAYS)
  580.     win.set_policy(hpol, vpol)
  581.     # do something about update policies here ...
  582.     return win
  583. def viewport_new(node):
  584.     port = GtkViewport()
  585.     shadow = node.get('shadow_type', SHADOW_IN)
  586.     port.set_shadow_type(shadow)
  587.     return port
  588.  
  589. def curve_new(node):
  590.     curve = GtkCurve()
  591.     curve.set_curve_type(node.get('curve_type', CURVE_TYPE_SPLINE))
  592.     minx = node.get_float('min_x', 0)
  593.     maxx = node.get_float('max_x', 1)
  594.     miny = node.get_float('min_y', 0)
  595.     maxy = node.get_float('max_y', 1)
  596.     curve.set_range(minx, maxx, miny, maxy)
  597.     return curve
  598. def gammacurve_new(node):
  599.     gamma = GtkGammaCurve()
  600.     # do something about the curve parameters ...
  601.     return gamma
  602. def colorselection_new(node):
  603.     cs = GtkColorSelection()
  604.     cs.set_update_policy(node.get('policy', UPDATE_CONTINUOUS))
  605.     return cs
  606. def preview_new(node):
  607.     type = node.get('type', PREVIEW_COLOR)
  608.     prev = GtkPreview(type)
  609.     prev.set_expand(node.get_bool('expand', TRUE))
  610.     return prev
  611.  
  612. def window_new(node):
  613.     wintype = node.get('type', 'toplevel')
  614.     widget = GtkWindow(wintype)
  615.     widget.set_title(node.get('title', node.name))
  616.     x = node.get_int('x', -1)
  617.     y = node.get_int('y', -1)
  618.     if x != -1 or y != -1:
  619.         widget.set_uposition(x, y)
  620.     pos = node.get('position', None)
  621.     if pos: widget.set_position(pos)
  622.     ashrink = node.get_bool('allow_shrink', TRUE)
  623.     agrow = node.get_bool('allow_grow', TRUE)
  624.     autoshrink = node.get_bool('auto_shrink', FALSE)
  625.     if not ashrink or not agrow or autoshrink:
  626.         widget.set_policy(ashrink, agrow, autoshrink)
  627.     return widget
  628. def dialog_new(node):
  629.     widget = GtkDialog()
  630.     widget.set_title(node.get('title', node.name))
  631.     x = node.get_int('x', -1)
  632.     y = node.get_int('y', -1)
  633.     if x != -1 or y != -1:
  634.         widget.set_uposition(x, y)
  635.     pos = node.get('position', None)
  636.     if pos: widget.set_position(pos)
  637.     ashrink = node.get_bool('allow_shrink', TRUE)
  638.     agrow = node.get_bool('allow_grow', TRUE)
  639.     autoshrink = node.get_bool('auto_shrink', FALSE)
  640.     if not ashrink or not agrow or autoshrink:
  641.         widget.set_policy(ashrink, agrow, autoshrink)
  642.     # do some weird stuff because the vbox/action area is already created
  643.     node.widget['__wid'] = widget.vbox
  644.     children = node.widget.widget
  645.     if type(children) != type(()): children = (children,)
  646.     for i in range(len(children)):
  647.         if children[i].has_key('child_name') and \
  648.            children[i].child_name == 'Dialog:action_area':
  649.             node['widget'] = (node.widget, children[i])
  650.             children[i].parent = node
  651.             children[i]['__wid'] = widget.action_area
  652.             node.widget[0]['widget'] = children[0:i]+children[i+1:]
  653.             break
  654.     return widget
  655. def colorselectiondialog_new(node):
  656.     return GtkColorSelectionDialog()
  657.  
  658.  
  659. _widgets = {
  660.     # widgets ...
  661.     'GtkLabel':        (label_new,        None),
  662.     'GtkEntry':        (entry_new,        None),
  663.     'GtkText':         (text_new,         None),
  664.     'GtkButton':       (button_new,       None),
  665.     'GtkToggleButton': (togglebutton_new, None),
  666.     'GtkCheckButton':  (checkbutton_new,  None),
  667.     'GtkRadioButton':  (radiobutton_new,  None),
  668.     'GtkOptionMenu':   (optionmenu_new,   None),
  669.     'GtkCombo':        (combo_new,        None),
  670.     'GtkList':         (list_new,         None),
  671.     'GtkCList':        (clist_new,        clist_add),
  672.     'GtkTree':         (tree_new,         None),
  673.     'GtkSpinButton':   (spinbutton_new,   None),
  674.     'GtkHScale':       (hscale_new,       None),
  675.     'GtkVScale':       (vscale_new,       None),
  676.     'GtkHRuler':       (hruler_new,       None),
  677.     'GtkVRuler':       (vruler_new,       None),
  678.     'GtkHScrollbar':   (hscrollbar_new,   None),
  679.     'GtkVScrollbar':   (vscrollbar_new,   None),
  680.     'GtkStatusbar':    (statusbar_new,    None),
  681.     'GtkToolbar':      (toolbar_new,      None),
  682.     'GtkProgressBar':  (progressbar_new,  None),
  683.     'GtkArrow':        (arrow_new,        None),
  684.     'GtkPixmap':       (pixmap_new,       None),
  685.     'GtkDrawingArea':  (drawingarea_new,  None),
  686.     'GtkHSeparator':   (hseparator_new,   None),
  687.     'GtkVSeparator':   (vseparator_new,   None),
  688.  
  689.     # Menu stuff ...
  690.     'GtkMenuBar':      (menubar_new,      menushell_add),
  691.     'GtkMenu':         (menu_new,         menushell_add),
  692.     'GtkMenuItem':     (menuitem_new,     menuitem_add),
  693.     'GtkCheckMenuItem':(checkmenuitem_new,menuitem_add),
  694.     'GtkRadioMenuItem':(radiomenuitem_new,menuitem_add),
  695.  
  696.     # Containers ...
  697.     'GtkHBox':         (hbox_new,         box_add),
  698.     'GtkVBox':         (vbox_new,         box_add),
  699.     'GtkTable':        (table_new,        table_add),
  700.     'GtkFixed':        (fixed_new,        fixed_add),
  701.     'GtkHButtonBox':   (hbuttonbox_new,   container_add),
  702.     'GtkVButtonBox':   (vbuttonbox_new,   container_add),
  703.     'GtkFrame':        (frame_new,        container_add),
  704.     'GtkAspectFrame':  (aspectframe_new,  container_add),
  705.     'GtkHPaned':       (hpaned_new,       paned_add),
  706.     'GtkVPaned':       (vpaned_new,       paned_add),
  707.     'GtkHandleBox':    (handlebox_new,    container_add),
  708.     'GtkNotebook':     (notebook_new,     notebook_add),
  709.     'GtkAlignment':    (alignment_new,    container_add),
  710.     'GtkEventBox':     (eventbox_new,     container_add),
  711.     'GtkScrolledWindow': (scrolledwindow_new, container_add),
  712.     'GtkViewport':     (viewport_new,     container_add),
  713.  
  714.     # Miscellaneous ...
  715.     'GtkCurve':        (curve_new,        None),
  716.     'GtkGammaCurve':   (gammacurve_new,   None),
  717.     'GtkColorSelection': (colorselection_new, None),
  718.     'GtkPreview':      (preview_new,      None),
  719.  
  720.     # Windows ...
  721.     'GtkWindow':       (window_new,       container_add),
  722.     'GtkDialog':       (dialog_new,       dialog_add),
  723.     'GtkColorSelectionDialog': (colorselectiondialog_new, None),
  724. }
  725.  
  726.  
  727.  
  728.